import java.util.PriorityQueue;
import java.util.TimerTask;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/*
public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        long c = (long)(a+b);
        System.out.println(c);
        long d = (long) a+b;
        System.out.println(d);
    }
}*/
/*class MyTimer{
    private PriorityQueue<MyTimerTask>queue = new PriorityQueue<>();
    private Object locker = new Object();
    public void schedule(Runnable runnable,long delay){
        synchronized (locker){
            queue.offer(new MyTimerTask(runnable,delay));
            locker.notify();
        }
    }
    public MyTimer(){
        Thread t = new Thread(()->{
            try{
                while (true) {
                    synchronized (locker) {
                        while (queue.isEmpty()) {
                            locker.wait();
                        }
                        MyTimerTask task = queue.peek();
                        long curTime = System.currentTimeMillis();
                        if (curTime >= task.getTime()) {
                            task.getRunnable().run();
                            queue.poll();
                        } else {
                            locker.wait(task.getTime() - curTime);
                        }
                    }
                }
            }catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t.start();
    }
}

class MyTimerTask implements Comparable<MyTimerTask>{
    private Runnable runnable;
    private long time;

    public MyTimerTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        this.time = System.currentTimeMillis()+delay;
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int) (this.time-o.time);
    }

    public long getTime() {
        return time;
    }
    public Runnable getRunnable(){
        return runnable;
    }
}

public class Test {
    public static void main(String[] args) {
        MyTimer timer = new MyTimer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("1000");
            }
        },1000);
    }
}*/
/*public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        service.submit(new Runnable() {
@Override
public void run() {
        System.out.println("hello");
        }
        });
        }*/
/*class MyThreadPool{
    private BlockingQueue<Runnable>queue = new ArrayBlockingQueue<>(1000);
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                try {
                    Runnable runnable = queue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
}*/
/*public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(4);
        for (int i = 0; i <1000; i++) {
        int id = i;
        myThreadPool.submit(new Runnable() {
@Override
public void run() {
        System.out.println("执行任务"+id);
        }
        });
        }
        }*/
/*    Thread t1 = new Thread(()->{
            for (int i = 0; i <50000; i++) {
        count.getAndIncrement();
        }
        });
        Thread t2 = new Thread(()->{
        for (int i = 0; i <50000; i++) {
        count.getAndIncrement();
        }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count.get());*/
/*public static void main(String[] args) throws InterruptedException, ExecutionException {
        Callable<Integer>callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i <=1000 ; i++) {
        sum+=i;
        }
        return  sum;
        }
        };
        FutureTask<Integer>futureTask=new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        System.out.println(futureTask.get());
        }*/
public class Test {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i <10 ; i++) {
            int id = i;
            Thread t = new Thread(()->{
                System.out.println("thread"+id);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            countDownLatch.countDown();
            t.start();
        }
        countDownLatch.await();
        System.out.println("所有的任务都完成了");
    }
}